#ifndef QLIB_QEDALIBRARY_H
#define QLIB_QEDALIBRARY_H

#include <QHash>
#include <QObject>

class LibraryTreeItem;
class QedaComponent;

class QedaLibrary : public QObject
{
    Q_OBJECT
public:
    explicit QedaLibrary(QObject *parent = 0);
    ~QedaLibrary();
    
    const QString &path() { return m_path; }
    void setPath(const QString &libPath) { m_path = libPath; }

    const QString &name() { return m_name; }
    void setName(const QString &libName) { m_name = libName; }

    bool load(const QString &filePath);
    bool save();
    const QList<QedaComponent*> &components() const { return m_components; }

    QedaComponent *createComponent();
    void addComponent(QedaComponent *component);
    bool removeComponent(QedaComponent *component);

    LibraryTreeItem *treeItem(int mode) { return m_treeItem.value(mode); }
    void setTreeItem(int mode, LibraryTreeItem *treeItem) { m_treeItem[mode] = treeItem; }

    bool isModified() { return m_modified; }
    void setModified(bool modified);

    QStringList references();
    void updateReferences(const QString &oldRef, const QString &newRef);
    QStringList groups();
    void updateGroups(const QString &oldGroup, const QString &newGroup);

    void removeAll();

signals:
    void componentsAdded(QedaLibrary *lib, const QList<QedaComponent*> &components);
    void componentUpdated(QedaComponent *component);
    void componentRemoved(QedaComponent *component);
    
public slots:
private:
    QedaComponent *newComponent();
    void appendComponent(QedaComponent *component);
    void addReference(const QString &ref);
    void removeReference(const QString &ref);
    void addGroup(const QString &group);
    void removeGroup(const QString &group);

private:
    bool                         m_modified;
    QString                      m_path;
    QString                      m_name;

    QList<QedaComponent*>        m_components;

    QHash<int,LibraryTreeItem*>  m_treeItem;
    QHash<QString, int>          m_references;
    QHash<QString, int>          m_groups;
};

#endif // QLIB_QEDALIBRARY_H
